/*
 * Copyright (c) 2025 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef OHOS_CEF_EXT_INCLUDE_CEF_CONTEXT_MENU_HANDLER_EXT_H_
#define OHOS_CEF_EXT_INCLUDE_CEF_CONTEXT_MENU_HANDLER_EXT_H_

#include "include/cef_base.h"
#include "include/cef_browser.h"
#include "include/cef_frame.h"
#include "include/cef_image.h"
#include "include/cef_menu_model.h"

class CefContextMenuHandlerExt : virtual public CefContextMenuHandler {
 private:
  using CefContextMenuHandler::OnQuickMenuDismissed;
  using CefContextMenuHandler::RunQuickMenu;

 public:
  ///
  /// Called to allow custom display of the quick menu for a windowless browser.
  /// |location| is the top left corner of the selected region. |size| is the
  /// size of the selected region. |edit_state_flags| is a combination of flags
  /// that represent the state of the quick menu. Return true if the menu will
  /// be handled and execute |callback| either synchronously or asynchronously
  /// with the selected command ID. Return false to cancel the menu.
  /// IS_OHOS extended
  ///
  virtual bool RunQuickMenu(CefRefPtr<CefBrowser> browser,
                            CefRefPtr<CefFrame> frame,
                            const CefPoint& location,
                            const CefSize& size,
                            const CefRect& select_bounds,
                            QuickMenuEditStateFlags edit_state_flags,
                            CefRefPtr<CefRunQuickMenuCallback> callback,
                            bool is_mouse_trigger,
                            bool is_long_press_actived) {
    return false;
  }

  ///
  /// UpdateClippedSelectionBounds
  /// IS_OHOS extended
  ///
  virtual bool UpdateClippedSelectionBounds(CefRefPtr<CefBrowser> browser,
                                            CefRefPtr<CefFrame> frame,
                                            const CefRect& select_bounds) {
    return false;
  }
  ///
  /// Called when the quick menu for a windowless browser is dismissed
  /// irregardless of whether the menu was canceled or a command was selected.
  /// IS_OHOS extended
  ///
  virtual void OnQuickMenuDismissed(CefRefPtr<CefBrowser> browser,
                                    CefRefPtr<CefFrame> frame,
                                    bool is_mouse_trigger) {}

  ///
  /// Called when GetImageForContextNode function get image for
  /// context menu.
  ///
  virtual void OnGetImageForContextNode(CefRefPtr<CefBrowser> browser,
                                        CefRefPtr<CefImage> image,
                                        int command_id) {}

  ///
  /// Called when GetImageFromCache function to get image from
  /// memory cache.
  ///
  virtual void OnGetImageFromCache(CefRefPtr<CefImage> image, int command_id) {}

  ///
  /// Called when GetImageFromCache function to get image from
  /// memory cache.
  ///
  /*--cef()--*/
  virtual void OnGetImageFromCacheEx(int command_id,
                                     uint8_t* data,
                                     int buffer_size) {}

  ///
  /// Called when you need to temporarily hide/restore the handle menu.
  ///
  virtual void HideHandleAndQuickMenuIfNecessary(bool hide) {}

  ///
  /// Called when you click on the selected area.
  ///
  virtual void ChangeVisibilityOfQuickMenu() {}

  ///
  /// Return is quick menu show.
  ///
  virtual bool IsQuickMenuShow() { return false; }

#if BUILDFLAG(ARKWEB_AI)
  ///
  /// Called to close selection on image overlay.
  ///
  /*--cef()--*/
  virtual bool CloseImageOverlaySelection() { return false; }
#endif

  CefRefPtr<CefContextMenuHandlerExt> AsCefContextMenuHandlerExt() override {
    return this;
  }

#if BUILDFLAG(ARKWEB_MENU)
  virtual void SetHandleVisibleCallback(
      const base::RepeatingCallback<void(bool)>& on_handle_visible) {}
  
  virtual void ShowMagnifier() {}
  
  virtual void HideMagnifier() {}

  virtual bool IsShowHandle() { return false; }
#endif

};

class CefContextMenuParamsExt : virtual public CefContextMenuParams {
 public:
  typedef cef_context_menu_input_field_type_t InputFieldType;
  typedef cef_context_menu_source_type_t SourceType;

  ///
  /// Returns the input field type of context node that the context menu was
  /// invoked on.
  ///
  virtual InputFieldType GetInputFieldType() = 0;

  ///
  /// Returns the source type of context node that the context menu was invoked
  /// on.
  ///
  virtual SourceType GetSourceType() = 0;

#if BUILDFLAG(ARKWEB_DRAG_DROP)
  ///
  /// Returns ImageRect.
  ///
  virtual void GetImageRect(int& x, int& y, int& w, int& h) = 0;

  ///
  /// Indicates whether the link tag is generated by AI.
  ///
  /*--cef()--*/
  virtual bool IsAILink() { return false; }
#endif

  CefRefPtr<CefContextMenuParamsExt> AsCefCefContextMenuParamsExt() override {
    return this;
  }
};

#endif
